var {randomUtils, clickUtils, logUtils} = require ('../../utils/index');
var config = require ('../../../config');
var {App} = require ('../../base/index');
var pointMap = require ('./pointMap');
var inTask = require ('./inTask');
var aRead = require ('./aRead');
var aTreasure = require ('./aTreasure');
var aWatchVideo = require ('./aWatchVideo');

var wuKong = new App ('悟空浏览器', {
  interval: 6 * 60,
  maxOpenCount: 5,
});

wuKong.runTest = function (count) {
  logUtils.info (this.name, 'runTest', count, '/', this.maxRunTCount);
  if (count == 0) {
    sleep (randomUtils.getRandom (10, 30, 1000));
    return;
  }
  if (count == 1) {
    if (randomUtils.possible (90, 100)) {
      clickUtils.clickPointMap (pointMap.首页.视频);
    } else if (randomUtils.possible (80, 90)) {
      clickUtils.clickPointMap (pointMap.首页.免费小说);
    } else if (randomUtils.possible (70, 80)) {
      clickUtils.clickPointMap (pointMap.首页.我的);
    } else {
      clickUtils.clickPointMap (pointMap.首页.我的金币);
    }
    sleep (randomUtils.getRandom (10, 30, 1000));
    return;
  }
  if (count == 2) {
    clickUtils.clickPointMap (pointMap.首页.视频);
    sleep (randomUtils.getRandom (10, 30, 1000));
  }
};
wuKong.run = function () {
  aRead.flow ();
  if (randomUtils.possible (95, 100)) {
    logUtils.print ('随机模式');
    if (randomUtils.possible (50, 100)) {
      clickUtils.clickPointMap (pointMap.首页.免费小说);
    } else {
      clickUtils.clickPointMap (pointMap.首页.我的);
    }
    sleep (randomUtils.getRandom (10, 30, 1000));
  }
  clickUtils.clickPointMap (pointMap.首页.我的金币);
  sleep (randomUtils.getRandom (10, 15, 1000));
  inTask.init ();
  if (config.wukong && !config.wukong.nowatch) {
    logUtils.print ('开始看视频');
    aWatchVideo.flow ();
    inTask.init ();
    sleep (randomUtils.getRandom (5, 10, 1000));
    if (!inTask.normal) {
      return;
    }
    if (randomUtils.possible (20, 100)) {
      logUtils.print ('开始看视频');
      aWatchVideo.flow ();
      inTask.init ();
      sleep (randomUtils.getRandom (5, 10, 1000));
    }
  }
  if (randomUtils.possible (5, 100)) {
    logUtils.print ('开始开宝箱');
    aTreasure.flow ();
    inTask.init ();
    sleep (randomUtils.getRandom (2, 5, 1000));
  }
  sleep (randomUtils.getRandom (5, 10, 1000));
};

module.exports = wuKong;
